//
//  PostWebContentViewController.m
//  PostWebContent
//

#import "PostWebContentViewController.h"

@implementation PostWebContentViewController

- (void)connection:(NSURLConnection *)connection
didReceiveResponse:(NSHTTPURLResponse *)response {
	[receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
	[receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
	
	UIAlertView *myAlert = [[UIAlertView alloc]
							initWithTitle:@"Error"
							message:[error localizedDescription]
							delegate:nil
							cancelButtonTitle:@"OK"
							otherButtonTitles:nil];
	[myAlert show];
	[myAlert release];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
	
	NSString *results = [[NSString alloc]
						 initWithBytes:[receivedData bytes]
						 length:[receivedData length]
						 encoding:NSASCIIStringEncoding];
	[resultsView loadHTMLString:results baseURL:[NSURL URLWithString:baseURL]];
	[results release];
}

- (void)viewDidLoad {
    [super viewDidLoad];
	
	CGRect resultsFrame = CGRectMake(10,10,300,440);
	resultsView = [[UIWebView alloc] initWithFrame:resultsFrame];
	[self.view addSubview:resultsView];
	
	baseURL = @"http://en.wikipedia.org";
	NSString *urlString = [baseURL stringByAppendingString:@"/w/index.php"];
	NSURL *url = [NSURL URLWithString:urlString];
	
	receivedData = [[NSMutableData alloc] initWithData:nil];
	NSMutableURLRequest *req = [[NSMutableURLRequest alloc]
								initWithURL:url
								cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
								timeoutInterval:30.0];
	
	[req setHTTPMethod:@"POST"];
	[req setHTTPBody:[@"search=iPhone" dataUsingEncoding:NSISOLatin1StringEncoding]];
	NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
	
	[req release];
	[conn release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)dealloc {
	
	[resultsView release];
	[receivedData release];
	
    [super dealloc];
}

@end
